package com.fqxiny.lease.web.app.service.impl;


import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fqxiny.lease.common.constant.RedisConstant;
import com.fqxiny.lease.common.exception.UpException;
import com.fqxiny.lease.common.result.ResultCodeEnum;
import com.fqxiny.lease.common.utils.JWTUtil;
import com.fqxiny.lease.model.entity.UserInfo;
import com.fqxiny.lease.web.app.mapper.UserInfoMapper;
import com.fqxiny.lease.web.app.service.LoginService;
import com.fqxiny.lease.web.app.service.UserInfoService;
import com.fqxiny.lease.web.app.vo.user.LoginVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author 25312
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Qualifier("redisTemplate")
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    UserInfoMapper userInfoMapper;
    @Override
    public void sendCode(String phone) {
        Object coded = redisTemplate.opsForValue().get(RedisConstant.ADMIN_LOGIN_PREFIX + phone);
        if(coded != null){
            throw new UpException(ResultCodeEnum.APP_SEND_SMS_TOO_OFTEN);
        }
        Random random = new Random();
        int randomNumber = 100000 + random.nextInt(900000); // 生成 [100000, 999999] 范围内的随机数
        String code = String.valueOf(randomNumber);
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setPhoneNumbers(phone);
        sendSmsRequest.setSignName("阿里云短信测试");
        sendSmsRequest.setTemplateCode("SMS_154950909");
        sendSmsRequest.setTemplateParam("{\"code\":\"" + code + "\"}\n");
        redisTemplate.opsForValue().set(RedisConstant.ADMIN_LOGIN_PREFIX +phone,code,RedisConstant.APP_LOGIN_CODE_RESEND_TIME_SEC, TimeUnit.SECONDS);
    }

    @Override
    public String login(LoginVo loginVo) {
        String code = loginVo.getCode();
        String phone = loginVo.getPhone();
        if(code == null){
            throw new UpException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
        String codeValue = (String) redisTemplate.opsForValue().get(phone);
        if(codeValue == null){
            throw  new UpException(ResultCodeEnum.APP_LOGIN_CODE_EXPIRED);
        }
        if(codeValue != code){
            throw new UpException(ResultCodeEnum.APP_LOGIN_CODE_ERROR);
        }
        UserInfo userInfo = userInfoMapper.selectOne(
                Wrappers.lambdaQuery(UserInfo.class)
                        .eq(UserInfo::getPhone, phone)
        );
        if(userInfo == null){
            userInfo = new UserInfo();
            userInfo.setNickname("用户"+phone);
            // todo : 用户初始密码加强
            userInfo.setPassword(phone);
            userInfoMapper.insert(userInfo);
        }
        return JWTUtil.createToken(userInfo.getId(),userInfo.getPhone());
    }
}
